package me.hl.concurrent;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by Planck on 2016/1/29.
 * corePollSize                         核心线程数量
 * maximumPoolSize               线程池中维护线程的最大数量
 * keepAliveTime                     线程池维护的非核心线程的空闲时间，空闲时间越过此值，就会被关闭
 * unit                                    线程池维护线程所允许的空闲时间的单位
 * workQueue                          线程池所使用的缓冲队列
 * threadFactory                      线程池中创建新线程的线程工厂
 * handler                               线程池对拒绝任务的处理策略
 */
public class ThreadPoolExecutorTest implements Runnable {

    private static final int CORE_SIZE = Runtime.getRuntime().availableProcessors();

    private static final int MAX_QUEUE_SIZE = 100;

    private int num;

    private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_SIZE,
            CORE_SIZE,
            0L,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(MAX_QUEUE_SIZE),
            new ThreadPoolExecutor.CallerRunsPolicy());

    public ThreadPoolExecutorTest(Integer num) {
        this.num = num;
    }


    public static void main(String args[]) {
        for (int i = 0; i < 100; i++) {
            System.out.println("strat up " + i);
            executor.execute(new ThreadPoolExecutorTest(i));
        }
    }


    @Override
    public void run() {
        System.out.println(num);
        try {
            Thread.sleep(1000);
        } catch (Exception e) {

        }

    }
}
